import request from "@/utils/request";
import store from "@/store/index";
/**
 * 判断当前用户是否为租户
 *
 * @returns {boolean} 当前用户是否为租户
 */
export function isTenant() {
  return !!store.getters.userInfo.tenantId;
}
/**
 * 设备上下文
 *
 * @returns {string} 设备上下文路径
 */
function deviceContext() {
  return isTenant() ? "/device" : "/ttdevice";
}
/**
 * 设备上下文3
 *
 * @returns {string} 设备上下文3的路径
 */
function deviceContext3() {
  return isTenant()
    ? "/device/iotdevice"
    : "/ttdevice/iotDeviceTT/ttview/device";
}
/**
 * 获取 BDM 上下文路径
 *
 * @returns {string} BDM 上下文路径
 */
function bdmContext() {
  return isTenant() ? "/bdm" : "/ttbdm";
}
// 获取喇叭--组织树
export function getHornMonitorOrgTree(data) {
  let url = "/ttdevice/hornTTDevice/getHornMonitorOrgTree";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceOrgTree";
    data.queryType = 5;
  }

  return request({
    data,
    url,
    method: "post",
  });
}
// 获取喇叭--区域树
export function getHornMonitorTree(data) {
  let url = "/ttdevice/hornTTDevice/getHornMonitorTree";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceAreaTree";
    data.queryType = 5;
  }
  return request({
    data,
    url,
    method: "post",
  });
}

//获取物联设备--区域树
export function getIotMonitorTree(data) {
  let url = "/ttdevice/iotDeviceTT/ttview/device/getIotMonitorTreeScreen";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceAreaTree";
    data.queryType = 4;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
//获取物联设备--组织树
export function getIotMonitorOrgTree(data) {
  let url = "/ttdevice/iotDeviceTT/getIotMonitorOrgTree";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceOrgTree";
    data.queryType = 4;
  }
  return request({
    data,
    url,
    method: "post",
  });
}

//获取雷达设备--区域树
export function getRadarMonitorTree(data) {
  let url = "/ttdevice/radarTT/getRadarMonitorTreeScreen";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceAreaTree";
    data.queryType = 3;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
//获取雷达设备--组织树
export function getRadarMonitorOrgTree(data) {
  let url = "/ttdevice/radarTT/getRadarMonitorOrgTree";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceOrgTree";
    data.queryType = 3;
  }
  return request({
    data,
    url,
    method: "post",
  });
}

//获取无人机--区域树
export function getUavMonitorAreaTree(data) {
  let url = "/uav/device/getUavMonitorAreaTree";
  if (isTenant()) {
    url = "/deviceCommon/getDeviceAreaTree";
    data.queryType = 1;
  }
  return request({
    data,
    url: deviceContext() + url,
    method: "post",
  });
}
//获取无人机--组织树
export function getUavMonitorOrgTree(data) {
  let url = "/ttdevice/uav/device/getUavMonitorDeptTree";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceOrgTree";
    data.queryType = 1;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
//获取喇叭详情
export function queryHornDeviceDetail(data) {
  let url = "/ttdevice/hornTTDevice/queryHornDeviceDetail";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceInfo";
    data.queryType = 5;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
//获取无人机详情
export function qryUavDeviceInfo(deviceCode) {
  let data,
    url = `/uav/device/qryUavDeviceInfo/${deviceCode}`;
  if (isTenant()) {
    url = "/deviceCommon/getDeviceInfo";
    data = {
      deviceCode,
      queryType: 1,
    };
  }
  return request({
    url: deviceContext() + url,
    method: "post",
    data,
  });
}
// 获取监测因子
export function getSensorData(data) {
  return request({
    data,
    url: deviceContext3() + `/getSensorData`,
    method: "post",
  });
}
// 获取监测数据
export function getSensorPeriodData(data) {
  return request({
    data,
    url: deviceContext3() + `/getSensorPeriodData`,
    method: "post",
  });
}

// 获取算法列表
export function queryByTenantId(data) {
  return request({
    data,
    url: "device/industry/algorithm/queryByTenantId",
    method: "post",
  });
}
// 摄像机点位信息
export function querySiteCodeAndDevices(data) {
  return request({
    data,
    url: deviceContext() + "/video/device/querySiteCodeAndDevices",
    method: "post",
  });
}
// 摄像机点位信息2
export function querySiteDevTree(data) {
  let url = "/ttdevice/hornTTDevice/querySiteDevTree";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceSites";
    data.queryType = 5;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
// 根据站址，查询喇叭列表
export function queryHornForSite(data) {
  let url = "/ttdevice/hornTTDevice/queryHornForSite";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceListBySiteCode";
    data.queryType = 5;
  }
  return request({
    data,
    url,
    method: "post",
  });
}

// 雷达点位信息
export function queryRadarDeviceByArea(data) {
  let url = "/ttdevice/radarTT/queryRadarDeviceByArea";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceSites";
    data.queryType = 3;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
// 根据站址，查询雷达列表
export function queryRadarDeviceBySite(data) {
  let url = "/ttdevice/radarTT/queryRadarDeviceBySite";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceListBySiteCode";
    data.queryType = 3;
  }
  return request({
    data,
    url,
    method: "post",
  });
}

// 物联设备点位信息
export function queryIotDeviceByArea(data) {
  let url = "/ttdevice/iotDeviceTT/queryIotDeviceByArea";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceSites";
    data.queryType = 4;
  }
  return request({
    data,
    url,
    method: "post",
  });
}
// 根据站址，查询物联设备列表
export function queryIotDeviceBySite(data) {
  let url = "/ttdevice/iotDeviceTT/queryIotDeviceBySite";
  if (isTenant()) {
    url = "/device/deviceCommon/getDeviceListBySiteCode";
    data.queryType = 4;
  }
  return request({
    data,
    url,
    method: "post",
  });
}

// 根据站址，查询无人机列表
export function queryUavDevicesBySiteCode(data) {
  let url = "/uav/device/queryUavDevicesBySiteCode";
  if (isTenant()) {
    url = "/deviceCommon/getDeviceListBySiteCode";
    data.queryType = 1;
  }
  return request({
    data,
    url: deviceContext() + url,
    method: "post",
  });
}

// 无人机点位信息
export function queryUavSitesTree(data) {
  let url = "/uav/device/queryUavSites";
  if (isTenant()) {
    url = "/deviceCommon/getDeviceSites";
    data.queryType = 1;
  }
  return request({
    data,
    url: deviceContext() + url,
    method: "post",
  });
}

// 铁塔-喇叭-图层统计
export function getHornNumForScreen(data) {
  return request({
    data,
    url: "/ttdevice/hornTTDevice/getHornNumForScreen",
    method: "post",
  });
}

// 铁塔-无人机-图层统计
export function getUavNumForScreen(data) {
  return request({
    data,
    url: "/ttdevice/uav/device/getUavNumForScreen",
    method: "post",
  });
}
// 铁塔-摄像机-图层统计
export function getNumForScreen(data) {
  return request({
    data,
    url: "/ttevent/geo/getNumForScreen",
    method: "post",
  });
}
// 铁塔-雷达-图层统计
export function getRadarDeviceNumForScreen(data) {
  return request({
    data,
    url: "/ttdevice/radarTT/getRadarDeviceNumForScreen",
    method: "post",
  });
}
// 铁塔-物联设备-图层统计
export function getIotDeviceNumForScreen(data) {
  return request({
    data,
    url: "/ttdevice/iotDeviceTT/getIotDeviceNumForScreen",
    method: "post",
  });
}

// 无人机设备区域树下具体设备
export function queryUavDevicesByAreaParam(data) {
  return request({
    data,
    url: "/ttdevice/uav/device/queryUavDevicesByAreaParam",
    method: "post",
  });
}
// 无人机设备组织树下具体设备
export function queryUavDevicesByDeptParam(data) {
  return request({
    data,
    url: "/ttdevice/uav/device/queryUavDevicesByDeptParam",
    method: "post",
  });
}
export function queryHornDevicesByDeptParam(data) {
  return request({
    data,
    url: "/ttdevice/hornTTDevice/queryHornDevicesByDeptParam",
    method: "post",
  });
}
export function queryRadarDevicesByDeptParam(data) {
  return request({
    data,
    url: "/ttdevice/radarTT/queryRadarDevicesByDeptParam",
    method: "post",
  });
}
export function queryIotDevicesByDeptParam(data) {
  return request({
    data,
    url: "/ttdevice/iotDeviceTT/queryIotDevicesByDeptParam",
    method: "post",
  });
}
/* 网格员信息详情 */
export function gridKeeper_(data) {
  return request({
    url: bdmContext() + `/gridKeeper/findOne?keeperId=${data.keeperId}`,
    method: "get",
  });
}

// 获取天气排行
export function airHistory(data) {
  return request({
    data,
    url: "/order/air/history",
    method: "post",
  });
}
export function getResourceInfo_(data) {
  return request({
    url: `/bdm/resource/getResourceInfo?resourceId=${data.resourceId}`,
    method: "get",
  });
}
//设备收藏
export function updateMonitorCollect(data, isRequestDevice) {
  // 构建 URL
  const baseUrl =
    isRequestDevice || isTenant() ? "/device/industry" : "/ttdevice/ttview";
  const url = `${baseUrl}/device/addOrCancelCollections`;
  // 发起请求
  return request({
    url,
    method: "post",
    data,
  });
}

//设备收藏
export function addOrCancelCollections(data) {
  return request({
    url: "/device/industry/device/addOrCancelCollections",
    method: "post",
    data,
  });
}
// 铁塔获取天气排行
export function queryWeatherHistoryData(data) {
  return request({
    data,
    url: "/ttorder/weather/queryWeatherHistoryData",
    method: "post",
  });
}
export function getAirQualityList(data) {
  return request({
    data,
    url: "/order/weather/getAirQualityList",
    method: "post",
  });
}

// 获取物联设备详情
export function queryIotDeviceInfo(data) {
  let url = "/ttdevice/iotDeviceTT/queryIotDeviceInfo";
  if (isTenant()) {
    url = "/device/iotdevice/queryIotDeviceInfo";
  }
  return request({
    data,
    url,
    method: "post",
  });
}
// 获取雷达详情
export function queryRadarDeviceInfo(data) {
  let url = "/ttdevice/radarTT/queryRadarDeviceInfo";
  if (isTenant()) {
    url = "/device/radar/queryRadarDeviceInfo";
  }
  return request({
    data,
    url,
    method: "post",
  });
}
